﻿@using Pear.Web.Core
@inherits PearRazorPage<object>
@{ Layout = "~/Views/Shared/_Layout.cshtml"; }
@{ ViewData["Title"] = "角色分配";}
@section header
{

}
<form class="layui-form" action="" lay-filter="user-edit">
    <div class="mainBox">
        <div class="main-container">

            <ul id="dtree" class="dtree" data-id="null" lay-filter="dtree"></ul>


        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="role-give">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
@section footer
{
    <script>
        layui.use(['form', 'jquery', 'http', 'popup','dtree'], function () {
            let form = layui.form;
            let $ = layui.jquery;
            let http = layui.http;
            let popup = layui.popup;
            let dtree = layui.dtree;





            var tree = dtree.render({
                elem: "#dtree",
                method: 'get',
                url: "/api/security/all",
                dataStyle: "layuiStyle",
                dataFormat: "list",
                checkbar: true,
                response: {
                    statusName: "statusCode",
                    statusCode: 200,
                    title: "name",
                    checkArr: "checkArr"
                },
                done: function (res, dom, first) {
                    if (first) {
                        http.get('/api/role/@GetValue("id","0")/securities').then((result) => {
                            if (result.succeeded) {
                                let chooses = [];
                                $.each(result.data, (i, item) => {
                                    chooses[i] = item.id;
                                });
                                dtree.chooseDataInit("dtree", chooses);
                            }
                        });
                    }
                }
            });



            form.on('submit(role-give)', function (data) {
                var params = dtree.getCheckbarNodesParam("dtree");

                let securitieIds = "";
                $.each(params,function (i,item) {
                    securitieIds += item.nodeId + ",";
                });
                securitieIds = securitieIds.substr(0, securitieIds.length - 1);
                var saveIds = new Array();
                saveIds = securitieIds.split(",");
                http.post('/api/security/give/@GetValue("id","0")', JSON.stringify(saveIds)).then((result) => {
                    if (result.succeeded) {
                        popup.success("修改成功", function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("role-table");
                        });
                    } else {
                        popup.failure("修改失败");
                    }
                });
                return false;
            });
        })
    </script>
}